package com.qf.sl.youshare.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.db.DataManager;
import com.qf.sl.youshare.model.Good;
import com.qf.sl.youshare.model.ProductCollect;
import com.qf.sl.youshare.model.UserLogin;
import com.qf.sl.youshare.tasks.GoodsTask;
import com.qf.sl.youshare.tasks.TaskProcessor;
import com.qf.sl.youshare.utils.FileCache;
import com.qf.sl.youshare.utils.ImageCache;
import com.qf.sl.youshare.utils.ImageLoader;
import com.qf.sl.youshare.utils.UserCurrent;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by IDEA
 * User : SL
 * on  2015/3/4.
 * 9:00
 */

/**
 * 平台商品显示界面
 */
public class PlatformGoodsActivity extends Activity implements TaskProcessor, View.OnClickListener {

    private ImageView imageView;
    private TextView priceView;
    private TextView standardView;
    private TextView nameView;
    private String goods_brief;
    private String goods_id;
    private String goods_type;
    private String thumb;
    private String price;
    private String goods_name;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_platform_goods);

        imageView = (ImageView) findViewById(R.id.goods_platform_img);
        priceView = (TextView) findViewById(R.id.goods_platform_youShare_price);
        standardView = (TextView) findViewById(R.id.goods_platform_youShare_standard);
        nameView = (TextView) findViewById(R.id.goods_platform_name);

        Button btnBuy = (Button) findViewById(R.id.goods_platform_youShare_buy);
        Button btnCollect = (Button) findViewById(R.id.goods_platform_youShare_collect);

        btnBuy.setOnClickListener(this);
        btnCollect.setOnClickListener(this);

        Intent intent = getIntent();

        String goodsId = intent.getStringExtra("goodsId");

        GoodsTask task = new GoodsTask(this);
        task.execute(goodsId);

    }

    @Override
    public void processResult(JSONObject jsonObject) {
        try {
            int code = jsonObject.getInt("code");
            if (code == 1){
                Good good = new Good();
                good.parseJSON(jsonObject);

                thumb = good.getGoods_thumb();
                price = good.getShop_price();
                String weight = good.getGoods_weight();
                goods_name = good.getGoods_name();

                goods_brief = good.getGoods_brief();
                goods_id = good.getGoods_id();
                goods_type = good.getGoods_type();

                System.out.println("price = " + price);

                if (price == null) {
                    price = good.getShop_price_formated();
                }
                if (price == null){
                    price = good.getMarket_price();
                }

                System.out.println("thumb = " + thumb);
                System.out.println("weight = " + weight);

                if (!(price.contains("￥"))){
                    price = "￥" + price;
                }
                if (!price.contains("元")){
                    price = price + "元";
                }

                nameView.setText(goods_name);
                standardView.setText(weight);
                priceView.setText(price);

                if (thumb != null && thumb.length() > 0) {
                    if (!thumb.startsWith("http://") && !thumb.startsWith("https://")){
                        thumb = Constants.IMAGE_SERVER + thumb;
                    }

                    imageView.setImageResource(R.drawable.product_default);

                    imageView.setTag(thumb);

                    ImageCache imageCache = ImageCache.getInstance();
                    Bitmap image = imageCache.getImage(thumb);
                    if (image != null) {
                        imageView.setImageBitmap(image);
                    }else {
                        FileCache fileCache = FileCache.getInstance();
                        byte[] bytes = fileCache.getFileContent(thumb);
                        if (bytes != null && bytes.length > 0) {
                            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                            imageView.setImageBitmap(bitmap);
                            imageCache.putImage(thumb, bitmap);
                        }else {
                            ImageLoader task = new ImageLoader(imageView);
                            task.execute(thumb);
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        Intent intent = null;
        switch (id){
            case R.id.goods_platform_youShare_buy:
                intent = new Intent(this, CartActivity.class);
                break;
            case R.id.goods_platform_youShare_collect:

                ProductCollect productCollect = new ProductCollect();

                productCollect.setDescription(goods_brief);
                productCollect.setName(goods_name);
                productCollect.setPid(goods_id);
                productCollect.setType(goods_type);
                productCollect.setThumb(thumb);

                String goods_price = price;
                if (goods_price.contains("￥")){
                    goods_price = goods_price.replace("￥","");
                }
                if (goods_price.contains("元")){
                    goods_price = goods_price.replace("元","");
                }
                productCollect.setPrice(Float.parseFloat(goods_price));

                String userId = Constants.DEFAULT_USER_ID;

                UserCurrent userCurrent = UserCurrent.getInstance();
                UserLogin userLogin = userCurrent.getUserLogin();
                if (userLogin != null) {
                    userId = userLogin.getUserId();
                }

                DataManager manager = DataManager.getInstance();
                int result = manager.productCollect(productCollect, userId);

                if (result == 1){
                    Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();
                    intent = new Intent(this, ProductCollectActivity.class);
                }else if (result == 2){
                    Toast.makeText(this, "已经收藏过", Toast.LENGTH_SHORT).show();
                    intent = new Intent(this, ProductCollectActivity.class);
                }else if (result == 3){
                    Toast.makeText(this, "数据库尚未开启", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        startActivity(intent);
    }
}